﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace WordsGame
{
    internal class StandingsProvider : IStandingsProvider
    {
        private string _fileName;

        public StandingsProvider(string fileName)
        {
            _fileName = fileName;
        }

        public void Save(Standings standings)
        {
            var serializer = new XmlSerializer(typeof(Standings));
            FileStream stream;
            if (!File.Exists(_fileName))
            {
                stream = File.Create(_fileName);
            }
            else
            {
                stream = File.OpenWrite(_fileName);
            }
            serializer.Serialize(stream, standings);
            stream.Close();
        }

        public Standings Load()
        {
            Standings result;
            if (File.Exists(_fileName))
            {
                var serializer = new XmlSerializer(typeof(Standings));
                result = serializer.Deserialize(File.OpenRead(_fileName)) as Standings;
            }
            else
            {
                result = new Standings();
            }

            return result;
        }
    }
}
